<?php
/*
 *      engine.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

require_once("errors.php");
require_once("config.php");
require_once("str.php");

require_once("request.class.php");
require_once("router.class.php");

require_once(SMARTY_DIR.'Smarty.class.php'); //Шаблонизатор Smarty

require_once("accounting.class.php");
require_once("access.class.php");
require_once("player.class.php");
require_once("messaging.class.php");
require_once("inventory.class.php");
require_once("map.class.php");
require_once("pmanager.class.php");
require_once("npc.class.php");
require_once("mobs.class.php");
require_once("controller.class.php");

require_once("c.idle.class.php");
require_once("c.front.class.php");
require_once("c.register.class.php");


class Engine
{
	static private $_instance = NULL;
	static private $_initiated = false;

	public $db;
	public $smarty;
	public $access;
	public $request;
	public $router;
	public $player;
	public $inventory;
	public $map;
	public $npc;
	public $messaging;
	public $mobs;

	static function get()
	{
		if (self::$_instance == NULL)
		{
		  self::$_instance = new self();
		}
		return self::$_instance;
	}

	private function __construct()
	{
		
	}

	private function __clone()
	{
	}

	public function init()
	{
		if(!self::$_initiated)
		{
			$this->_connectDatabase();
			$this->smarty = new Smarty();
			$this->_setupSmarty();
			$this->_accessControl();
			Engine::get()->constructPlayer($this->access->getId());
			$this->pmanager = new PManager();
			$this->request = new Request();
			$this->router = new Router($this->request);
		}
	}
	
	protected function _accessControl()
	{
		$this->access = new Access();
	}
	
	protected function _connectDatabase()
	{
		try {
			$this->db = new PDO(CONFIG_DB_DSN, CONFIG_DB_LOGIN, CONFIG_DB_PASSWORD);
			$stmt = Engine::get()->db->prepare('SET NAMES UTF8;'); //Принудительная установка кодировки соединения
			$stmt->execute();
		} catch (PDOException $e) {
		throw new Database_Exception($e->getMessage());
		}
	}
	
	protected function _setupSmarty()
	{
		$this->smarty->template_dir = DIR_TEMPLATES.'/tpl/';
		$this->smarty->compile_dir = DIR_TEMPLATES.'/compile/';
		$this->smarty->config_dir = DIR_TEMPLATES.'/config/';
		$this->smarty->cache_dir = DIR_TEMPLATES.'/cache/';
		$this->smarty->assign('str',STR::$str);
		if (CONFIG_DEBUG_SMARTY === true)
		{
			$this->smarty->debugging = true;
		}
		else
		{
			$this->smarty->debugging = false;
		}
	}
	
	public function constructPlayer($id)
	{
		$this->player = new Player($id);
		$this->messaging = new Messaging($id);
		$this->inventory = new Inventory($id);
	}
	
	public function loadMap($coords)
	{
		if (!isset($coords))
		{
			if (isset($this->player))
			{
				$coords = $this->player->coords();
			}
			else
			{
				throw new Application_Exception('Constructive error. Inform developer.');
			}
		}
		if (!$this->map)
		{
			$this->map = new Map($coords['X'],$coords['Y'],$coords['Z'],$coords['L']);
		}
		else
		{
			$this->map->load($coords['X'],$coords['Y'],$coords['Z'],$coords['L']);
		}
		$this->loadNPC($coords);
		$this->loadMobs($coords);
	}
	
	protected function loadNPC($coords)
	{
		$this->npc = new NPC($coords);
	}
	
	protected function loadMobs($coords)
	{
		$this->mobs = new Mobs($coords);
	}
	
	
}

Engine::get()->init();

?>
